package com.songtech.uitest.module.propose;

import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.AlertHandler;
import com.songtech.uitest.module.common.KakongHandler;
import com.songtech.uitest.module.common.MenuHandler;
import com.songtech.uitest.module.common.TabHandler;
import com.songtech.uitest.resources.IndentResource;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class UndertakeTower {

    @Autowired
    @Qualifier("indentResource")
    private IndentResource indentResource;

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private AlertHandler alertHandler;

    @Autowired
    private StepParser stepParser;

    private Logger logger = LoggerFactory.getLogger(UndertakeTower.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;


    public void undertakeTower(String step) throws Exception{

        boolean approval = stepParser.parseApproval(step);
        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //打开模拟页
        menuHandler.clickMenuItem(driver, "接口模拟", "铁塔反馈需求承接结果", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);

        logger.info(space.getSpace() + "开始填写需求承接表单");

        //填表单
        driver.findElement(By.id("towerProposeCode")).sendKeys(indent.getTowerId());

        driver.findElement(By.id("province")).clear();
        driver.findElement(By.id("province")).sendKeys(indentResource.getUndertakeProvince());
        driver.findElement(By.id("region")).clear();
        driver.findElement(By.id("region")).sendKeys(indentResource.getUndertakeRegion());
        driver.findElement(By.id("county")).clear();
        driver.findElement(By.id("county")).sendKeys(indentResource.getUndertakeCountry());
        //是否承接
        Select requestState = new Select(driver.findElement(By.id("requestState")));
        requestState.selectByVisibleText(approval ? "是" : "否");

        driver.findElement(By.id("custName")).sendKeys("铁塔侧确认人");
        driver.findElement(By.id("failMessage")).sendKeys("退回原因如此如此");

        driver.findElement(By.id("saveSet")).click();
        Thread.sleep(5000);

        alertHandler.checkAndGo(driver);

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        String take = approval ? "承接" : "不承接";
        logger.info(space.getSpace() + "需求承接表单提交完成，承接结果是" + take);

        tabHandler.closeAllTabs(driver);

    }


    public void undertake3Tower(String step) throws Exception{

        boolean approval = stepParser.parseApproval(step);
        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //打开模拟页
        menuHandler.clickMenuItem(driver, "铁塔建设管理", "需求承接", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);


        //查询此单
        driver.findElement(By.id("keywords")).sendKeys(indent.getMobileId());
        driver.findElement(By.xpath("//button[text()='查询']")).click();
        Thread.sleep(6000);
        //选择最新的一条进行提交
        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input")).click();
        driver.findElement(By.xpath("//button[text()='处理']")).click();
        //转到需求承接详细页
        Thread.sleep(5000);
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);
        //回到需求承接详细页上
        WebElement frame = driver.findElements(By.name("iframe0")).get(1);
        driver.switchTo().frame(frame);

        //选择承接与否
        Select status = new Select(driver.findElement(By.id("status")));
        status.selectByVisibleText(approval ? "承接" : "不承接");

        Thread.sleep(1000);
        if(approval == false){
            driver.findElement(By.id("remark")).sendKeys("不承接，不解释");
        }

        driver.findElement(By.id("saveSet")).click();

        Thread.sleep(5000);

        alertHandler.checkAndGo(driver);

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        String take = approval ? "承接" : "不承接";
        logger.info(space.getSpace() + "三方需求承接完成，承接结果是" + take);

        tabHandler.closeAllTabs(driver);

    }

}
